package com.zjitc.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduCourse;
import com.zjitc.serviceedu.entity.EduTeacher;
import com.zjitc.serviceedu.entity.vo.CourseInfoVo;
import com.zjitc.serviceedu.entity.vo.CourseListVo;
import com.zjitc.serviceedu.entity.vo.CoursePublishVo;
import com.zjitc.serviceedu.entity.vo.TeacherVo;
import com.zjitc.serviceedu.service.EduCourseDescriptionService;
import com.zjitc.serviceedu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/serviceedu/edu-course")
@Api(tags = "课程模块")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;


    @ApiOperation("添加课程基本信息")
    @PostMapping("addCourseInfo")
    public RespBean addCourseInfo(CourseInfoVo courseInfoVo) {
        int id = courseService.saveCourseInfo(courseInfoVo);
        return RespBean.success().data("courseId", id).msg("成功");
    }

    @ApiOperation("修改详情查询")
    @GetMapping("getCourseInfo/{courseId}")
    public RespBean getCourseInfo(@PathVariable Long courseId) {
        CourseInfoVo course = courseService.getCourseInfo(courseId);
        if (course == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", course).msg("查询成功");
    }
    @ApiOperation("根据id查询")
    @GetMapping("getCourseId/{courseId}")
    public RespBean getCourseId(@PathVariable Long courseId) {
        EduCourse data = courseService.getById(courseId);
        if (data == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", data).msg("查询成功");
    }

    @ApiOperation("修改课程")
    @PutMapping("updateCourse")
    public RespBean updateCourse(CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return RespBean.success().msg("修改成功");
    }

    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{id}")
    public RespBean removeById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable Long id){

        boolean result = courseService.removeByCouresId(id);
        if(result){
            return RespBean.success().data("result",result).msg("删除成功");
        }else{
            return RespBean.error(201,"删除失败");
        }
    }

    @ApiOperation("批量删除课程")
    @DeleteMapping("delMoreCourse")
    public RespBean delMoreRole(Long[] ids) {
        boolean result = courseService.removeByIds(Arrays.asList(ids));
        if (!result) {
            throw new AuthException("批量删除失败");
        }

        return RespBean.success().msg("批量删除成功");
    }

    @ApiOperation("课程发布信息")
    @GetMapping("getCoursePublishInfo/{id}")
    public RespBean getCoursePublishInfo(@PathVariable Long id) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishInfo(id);
        return RespBean.success().data("data", coursePublishVo).msg("查询成功");
    }

    @ApiOperation("课程多条件查询")
    @GetMapping("getCourseCondition")
    public RespBean getCourseCondition(CourseListVo courseListVo, @RequestParam(required = false) String sortField, @RequestParam(required = false) String sortOrder) {
        Page<EduCourse> page = new Page<>(courseListVo.getPage(), courseListVo.getSize());
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();

        // 处理其他条件过滤
        if (courseListVo.getTitle() != null && !courseListVo.getTitle().isEmpty()) {
            queryWrapper.like("title", courseListVo.getTitle());
        }
        if (courseListVo.getPrice() != null) {
            queryWrapper.eq("price", courseListVo.getPrice());
        }
        if (courseListVo.getBuyCount() != null) {
            queryWrapper.eq("buy_count", courseListVo.getBuyCount());
        }
        if (courseListVo.getViewCount()!= null) {
            queryWrapper.eq("view_count", courseListVo.getViewCount());
        }
        if (courseListVo.getGmtCreate() != null) {
            queryWrapper.eq("gmt_create", courseListVo.getGmtCreate());
        }
        if (courseListVo.getTeacherId() != null) {
            queryWrapper.eq("teacher_id", courseListVo.getTeacherId());
        }
        if (courseListVo.getSubjectId() != null) {
            queryWrapper.eq("subject_id", courseListVo.getSubjectId());
        }
        if (courseListVo.getSubjectParentId() != null) {
            queryWrapper.eq("subject_parent_id", courseListVo.getSubjectParentId());
        }

        // 动态排序处理
        if (sortField != null && !sortField.isEmpty()) {
            if ("asc".equalsIgnoreCase(sortOrder)) {
                queryWrapper.orderByAsc(sortField);
            } else {
                queryWrapper.orderByDesc(sortField);
            }
        } else {
            // 默认排序，按 ID 倒序
            queryWrapper.orderByDesc("id");
        }

        courseService.page(page, queryWrapper);

        List<EduCourse> list = page.getRecords();
        Long total = page.getTotal();

        if (list.isEmpty()) {
            return RespBean.success().data("list", list).data("total", total).msg("未找到符合条件的课程");
        }

        return RespBean.success().data("list", list).data("total", total).msg("查询成功");
    }

    //查询热门课程
    @ApiOperation("查询热门课程")
    @GetMapping("getHotList")
    public RespBean getHotList(){
        List<EduCourse> eduList = courseService.getHotList();
        return  RespBean.success().data("eduList",eduList).msg("查询热门课程");
    }
    //查询java系列课程
    @ApiOperation("java课程查询")
    @GetMapping("getJavaList")
    public RespBean getJavaList(){

        List<EduCourse> eduList = courseService.getJavaList();
        return  RespBean.success().data("eduList",eduList).msg("查询java课程成功");
    }

    //查询java系列课程
    @ApiOperation("H5Css课程查询")
    @GetMapping("getCssList")
    public RespBean getCssList(){

        List<EduCourse> eduList = courseService.getCssList();
        return  RespBean.success().data("eduList",eduList).msg("查询css课程成功");
    }

    @ApiOperation("讲师课程查询")
    @GetMapping("teacherCourse/{id}")
    public RespBean teacherCourse(@PathVariable Long id){
        List<EduTeacher> teacherCourse = courseService.getTeacherCourse(id);
        return  RespBean.success().data("teacherCourse",teacherCourse).msg("查询讲师课程成功");
    }

    @ApiOperation("更新课程浏览量")
    @PostMapping("/updateViewCount/{courseId}")
    public RespBean updateViewCount(@PathVariable Long courseId) {
        // 调用 service 层来更新 viewCount
        boolean success = courseService.updateViewCount(courseId);
        if (success) {
            return RespBean.success().msg("浏览量更新成功");
        } else {
            return RespBean.error(201,"更新失败");
        }
    }


    @ApiOperation("更新课程购买量")
    @PostMapping("/updateBuyCount/{courseId}")
    public RespBean updateBuyCount(@PathVariable Long courseId) {
        // 调用 service 层来更新 viewCount
        boolean success = courseService.updateBuyCount(courseId);
        if (success) {
            return RespBean.success().msg("购买量更新成功");
        } else {
            return RespBean.error(201,"更新失败");
        }
    }
}


